package eths

import (
	"fmt"
	"math/big"

	"go-copyright/configs"

	"go-copyright/utils"

	"os"

	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/ethereum/go-ethereum/rpc"
)

//创建一个以太坊账户
func NewAcc(pwd, connstr string) (string, error) {
	//链接geth,获取以太坊conn对象
	conn, err := rpc.Dial(connstr)
	if err != nil {
		fmt.Println("filed to connect geth -> ", err)
		return "", err
	}

	//创建账户
	var account string
	err = conn.Call(&account, "personal_newAccount", pwd)
	if err != nil {
		fmt.Println("filed to create geth account -> ", err)
		return "", err
	}
	fmt.Println("acc == ", account)
	return account, nil
}

//挖矿(UploadPic->Mint)
func UploadPic(from, pwd, hash, data string) error {
	//dial
	cli, err := ethclient.Dial(configs.Config.Eth.Connstr)
	if err != nil {

		fmt.Println("faild to geth error", err)
		return err
	}
	//入口
	//获取文件名称
	fileName, err := utils.GetFileName(string([]rune(from)[2:]), configs.Config.Eth.Keydir)

	if err != nil {
		fmt.Println("faild to GetFileName", err)
		return err
	}

	instance, err := NewPxa(common.HexToAddress(configs.Config.Eth.PxaAddr), cli)
	if err != nil {
		fmt.Println("faild to NewPxa", err)
		return err
	}
	//设置签名
	file, err := os.Open(configs.Config.Eth.Keydir + "/" + fileName)
	if err != nil {
		fmt.Println("faild to open file", err)
		return err
	}
	auth, err := bind.NewTransactor(file, pwd)
	if err != nil {
		fmt.Println("faild to NewTransactor", err)
		return err
	}
	//调用Mint
	//function mint(string _hash,uint _price,uint _weight,string _metaData)
	_, err = instance.Mint(auth, common.HexToHash(hash), big.NewInt(100), big.NewInt(100), data)
	if err != nil {
		fmt.Println("faild to Mint", err)
		return err
	}
	return nil
}

//资产分割(foundation基金会签名)
func EthSplitAsset(foundation, pwd, buyer string, tokenID, weight int64) error {
	//dial
	cli, err := ethclient.Dial(configs.Config.Eth.Connstr)
	if err != nil {

		fmt.Println("faild to geth error", err)
		return err
	}
	//入口
	//获取文件名称
	fileName, err := utils.GetFileName(string([]rune(foundation)[2:]), configs.Config.Eth.Keydir)

	if err != nil {
		fmt.Println("faild to GetFileName", err)
		return err
	}

	instance, err := NewPxa(common.HexToAddress(configs.Config.Eth.PxaAddr), cli)
	if err != nil {
		fmt.Println("faild to NewPxa", err)
		return err
	}
	//设置签名
	file, err := os.Open(configs.Config.Eth.Keydir + "/" + fileName)
	if err != nil {
		fmt.Println("faild to open file", err)
		return err
	}
	auth, err := bind.NewTransactor(file, pwd)
	if err != nil {
		fmt.Println("faild to NewTransactor", err)
		return err
	}
	//调用splitAsset
	_, err = instance.SplitAsset(auth, big.NewInt(tokenID), big.NewInt(weight), common.HexToAddress(buyer))
	if err != nil {
		fmt.Println("faild to SplitAsset", err)
		return err
	}
	return nil
}

//erc20转账       from:buyer   to:发起拍卖的人(mysql)
func EthTransfer20(from, pwd, seller string, price int64) error {
	//dial
	cli, err := ethclient.Dial(configs.Config.Eth.Connstr)
	if err != nil {

		fmt.Println("faild to geth error", err)
		return err
	}
	//入口
	//获取文件名称
	fileName, err := utils.GetFileName(string([]rune(from)[2:]), configs.Config.Eth.Keydir)

	if err != nil {
		fmt.Println("faild to GetFileName", err)
		return err
	}

	instance, err := NewPxc(common.HexToAddress(configs.Config.Eth.PxcAddr), cli)
	if err != nil {
		fmt.Println("faild to NewPxa", err)
		return err
	}
	//设置签名
	file, err := os.Open(configs.Config.Eth.Keydir + "/" + fileName)
	if err != nil {
		fmt.Println("faild to open file", err)
		return err
	}
	auth, err := bind.NewTransactor(file, pwd)
	if err != nil {
		fmt.Println("faild to NewTransactor", err)
		return err
	}
	//调用splitAsset
	_, err = instance.Transfer(auth, common.HexToAddress(seller), big.NewInt(price))
	if err != nil {
		fmt.Println("faild to Transfer", err)
		return err
	}
	return nil
}
